package cn.dai.Lambda;

import cn.dai.Reflection.Person;
import org.junit.Test;

import java.util.Comparator;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author ArkD42
 * @file 1 - Java基础语法
 * @create 2020 - 04 - 23 - 14:37
 */
public class MethodReference {

    // 对象::实例方法
    @Test
    public void mr(){
        Consumer<String> consumer1 = string -> System.out.println(string);
        consumer1.accept("杰哥不要啊");

        //----------------------------------------------------------

        // 方法引用 本质还是Lambda表达式，Lambda表述作为函数式接口的实例
        Consumer<String> consumer2 = System.out::println; // PrintStream void println(T t);
        consumer2.accept("杰哥不要啊");
    }

    @Test
    public void mr2(){
        Person person = new Person("阿伟",25,true);
        Supplier<String> supplier = person::getName;
        System.out.println(supplier.get());
    }

    // 类::静态方法
    @Test
    public void mr3(){
        // Integer  compare(10, 5);
        // Comparator   compare(o1,o2);
        Comparator<Integer> comparator1 = (o1,o2) -> Integer.compare(o1,o2);
        Comparator<Integer> comparator2 = Integer::compare;
        int compare = comparator2.compare(10, 5);
    }

    @Test
    public void mr4(){
        // Function R apply(T t);
        // Math Long round(Double d);
        Function<Double,Long> function1 = d -> Math.round(d);
        Function<Double,Long> function2 = Math::round;
    }

    @Test
    public void mr5(){
        Comparator<String> comparator1 = (str1,str2) -> str1.compareTo(str2);
        Comparator<String> comparator2 = String::compareTo;
    }

    @Test
    public void mr6(){
        BiPredicate<String,String> predicate1 = (s1,s2) -> s1.equals(s2);
        BiPredicate<String,String> predicate2 = String :: equals;
    }

    @Test
    public void mr7(){
        Person person = new Person("阿伟",25,true);

        Function<Person,String> function1 = p -> p.getName();
        Function<Person,String> function2 = Person::getName;

        System.out.println(function2.apply(person));
    }

    @Test
    public void mr8(){
        Supplier<Person> supplier1 = ()-> new Person();
        Supplier<Person> supplier2 = Person::new;

        Person person1 = supplier1.get();
        Person person2 = supplier2.get();

        Function<String,Person> function1 = (name)-> new Person(name);
        Function<String,Person> function2 = Person::new;

        Person person3 = function1.apply("阿强");
        Person person4 = function2.apply("阿伟");

        Function<Integer,String[]> function3 = length -> new String[length];
        Function<Integer,String[]> function4 = String[]::new;

        String[] strings1 = function3.apply(10);

    }




}
